package base.db.lib.back;

import java.io.BufferedReader;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
public class DBReader {
	

	protected ResultSet resultset;
	protected ResultSetMetaData metadata;
	protected String[] fields = new String[0]; //列名名称数组
	private List resultList =null; // 其中装载 Map集合 
	

	public void executeQuery(ResultSet resultSet) throws Exception {
			this.resultset =resultSet;
			this.metadata = this.resultset.getMetaData();
			getDataFromResultSetMap();
	}

	/** 
	 * resultList 设置值  其中装载 Map的数组   返回查询结果
	 * @param p_iStartRow
	 * @param p_iEndRow
	 * @throws Exception
	 */
	private void getDataFromResultSetMap()
			throws Exception {
		int numberOfColumns = this.metadata.getColumnCount();
		this.fields = new String[numberOfColumns];
        //设置字段
		for (int column = 0; column < numberOfColumns; ++column) {
			this.fields[column] = this.metadata.getColumnName(column + 1);
		}
       //设置结果值 
		this.resultList = new ArrayList();
		while (this.resultset.next()) {
			
			Map newRow = new HashMap();
			for (int i = 1; i <= numberOfColumns; ++i) {
				String typeName = this.metadata.getColumnTypeName(i);
				String key = "";
				String value = "";
				key = fields[i-1].toLowerCase(); // 将字段名称设置为统一为小写  并且设置为Map的key的值
				if (typeName.equals("NUMBER")) { //数字类型
					try {
						BigDecimal bd = this.resultset.getBigDecimal(i);
						value = bd.toString();
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("数字类型转换异常！");
						continue;
					}
				}else if(typeName.equals("CLOB")){//大文本类型
					try {
						java.sql.Clob clob = this.resultset.getClob(i);
						StringBuilder content=new StringBuilder("");
						if (clob != null) {
							Reader is = clob.getCharacterStream();
							BufferedReader br = new BufferedReader(is);				
							String s = br.readLine();
							while (s != null) {
								if(s!=null){
							//  String temp=new String(s.getBytes("ISO-8859-1"), "utf-8");
							    content.append(s);
								s = br.readLine();
								}
							}
							br.close();
							is.close();
						}
						value = content.toString();
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("大文本类型转换异常！");
						continue;
					}	 
				} else {
					value = this.resultset.getString(i);
					if(value==null ||value==""){
						value="";
					}
					// 输出时间去掉最后"."符号
					if (value.indexOf("-") != -1 && value.indexOf(" ") != -1
							&& value.indexOf(":") != -1
							&& value.indexOf(".") != -1) {
						value = value.substring(0, value.lastIndexOf("."));	
					}
				}
				//填充数据
				newRow.put(key, value);
			}
			this.resultList.add(newRow);
		}
	}
	
	public int getColumnCount() {
		return this.fields.length;
	}
	public int getRowCount() {
		return this.resultList !=null ? this.resultList.size():0;
	}
	public String[] getFields() {
		return this.fields;
	}
	public List getResultList() {
		return resultList;
	}
	
}
